package GUILayer;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import CtrLayer.SaleCtr;
import ModelLayer.Employee;
import ModelLayer.PartSale;
import ModelLayer.Sale;

/**
 * PaymentGUI.
 * This class creates the paymentGUIPanel which contains the elements needed to complete a sale.
 * 31.05.2012
 * @author Lindgren
 * @Version 0.1
 */
public class PaymentGUI  {


	private Employee employee;
	private JPanel paymentGUIPanel;

	private JTextField total;
	private JTextField paid;
	private JTextField change;
	private JTextArea salesList;

	/**
	 * Create the paymentGUI.
	 */
	public PaymentGUI(Employee employee) {

		this.employee = employee; 
		initialize();
		System.out.println("PaymentGUI initialized!");
	}


	/**
	 * initializes the paymentGUIPanel
	 */
	private void initialize() {

		System.out.println("PaymentGUI initializing");

		paymentGUIPanel = new JPanel();
		paymentGUIPanel.setToolTipText("paymentGUIPanel");
		paymentGUIPanel.setForeground(Color.BLACK);
		paymentGUIPanel.setName(employee.getLogin());
		MainFrame.getPaymentGUIPanels().put(employee.getLogin(), paymentGUIPanel);
		System.out.println("PaymentGUI paymentGUIPanel mapped: "+MainFrame.getPaymentGUIPanels().get(employee.getLogin()));
		paymentGUIPanel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel TopPanel = new JPanel();
		paymentGUIPanel.add(TopPanel);
		TopPanel.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel = new JPanel();
		TopPanel.add(panel);
		panel.setBorder(new TitledBorder(null, "Sales List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		salesList = new JTextArea();
		salesList.setEditable(false);
		panel.add(salesList);

		JPanel infoPanel = new JPanel();
		TopPanel.add(infoPanel);
		infoPanel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_15 = new JPanel();
		infoPanel.add(panel_15);

		JLabel label = new JLabel("");
		infoPanel.add(label);

		JLabel label_1 = new JLabel("");
		infoPanel.add(label_1);

		JPanel panel_9 = new JPanel();
		infoPanel.add(panel_9);
		panel_9.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblTotal = new JLabel("Total:");
		lblTotal.setHorizontalAlignment(SwingConstants.CENTER);
		panel_9.add(lblTotal);

		total = new JTextField();
		total.setText("0.0");
		total.setEditable(false);
		total.setBackground(UIManager.getColor("Button.background"));
		total.setHorizontalAlignment(SwingConstants.TRAILING);
		panel_9.add(total);
		total.setColumns(10);

		JLabel lblKr_2 = new JLabel("Kr");
		lblKr_2.setHorizontalAlignment(SwingConstants.CENTER);
		panel_9.add(lblKr_2);

		JPanel panel_8 = new JPanel();
		infoPanel.add(panel_8);
		panel_8.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblPaid = new JLabel("Paid:");
		lblPaid.setHorizontalAlignment(SwingConstants.CENTER);
		panel_8.add(lblPaid);

		paid = new JTextField();
		paid.setBackground(Color.WHITE);
		paid.setEditable(false);
		paid.setText("0.0");
		paid.setHorizontalAlignment(SwingConstants.TRAILING);
		panel_8.add(paid);
		paid.setColumns(10);

		JLabel lblKr = new JLabel("Kr");
		lblKr.setHorizontalAlignment(SwingConstants.CENTER);
		panel_8.add(lblKr);

		JPanel panel_7 = new JPanel();
		infoPanel.add(panel_7);
		panel_7.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblChange = new JLabel("Change:");
		lblChange.setHorizontalAlignment(SwingConstants.CENTER);
		panel_7.add(lblChange);

		change = new JTextField();
		change.setText("0.0");
		change.setEditable(false);
		change.setBackground(UIManager.getColor("Button.background"));
		change.setHorizontalAlignment(SwingConstants.TRAILING);
		panel_7.add(change);
		change.setColumns(10);

		JLabel lblKr_1 = new JLabel("Kr");
		lblKr_1.setHorizontalAlignment(SwingConstants.CENTER);
		panel_7.add(lblKr_1);

		JPanel panel_16 = new JPanel();
		infoPanel.add(panel_16);

		JPanel panel_14 = new JPanel();
		infoPanel.add(panel_14);
		panel_14.setLayout(null);

		JLabel label_2 = new JLabel("");
		infoPanel.add(label_2);

		JLabel label_3 = new JLabel("");
		infoPanel.add(label_3);

		JPanel numericKeyboardPanel = new JPanel();
		TopPanel.add(numericKeyboardPanel);
		numericKeyboardPanel.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel panel_3 = new JPanel();
		numericKeyboardPanel.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 1, 0, 0));

		JButton button = new JButton("50");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterQuickAmount("50");
				enterPayment();
			}
		});
		panel_3.add(button);

		JButton button_1 = new JButton("100");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterQuickAmount("100");
			}
		});
		panel_3.add(button_1);

		JButton button_2 = new JButton("200");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterQuickAmount("200");
			}
		});
		panel_3.add(button_2);

		JButton button_3 = new JButton("500");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterQuickAmount("500");
			}
		});
		panel_3.add(button_3);

		JButton button_4 = new JButton("1000");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterQuickAmount("1000");
			}
		});
		panel_3.add(button_4);

		JPanel panel_2 = new JPanel();
		numericKeyboardPanel.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_1 = new JPanel();
		panel_2.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 3, 0, 0));

		JButton button_12 = new JButton("7");
		button_12.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("7");
			}
		});
		panel_1.add(button_12);

		JButton button_13 = new JButton("8");
		button_13.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("8");
			}
		});
		panel_1.add(button_13);

		JButton button_14 = new JButton("9");
		button_14.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("9");
			}
		});
		panel_1.add(button_14);

		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 3, 0, 0));

		JButton button_9 = new JButton("4");
		button_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("4");
			}
		});
		panel_4.add(button_9);

		JButton button_10 = new JButton("5");
		button_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("5");
			}
		});
		panel_4.add(button_10);

		JButton button_11 = new JButton("6");
		button_11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("6");
			}
		});
		panel_4.add(button_11);

		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 3, 0, 0));

		JButton button_6 = new JButton("1");
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("1");
			}
		});
		panel_5.add(button_6);

		JButton button_7 = new JButton("2");
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("2");
			}
		});
		panel_5.add(button_7);

		JButton button_8 = new JButton("3");
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("3");
			}
		});
		panel_5.add(button_8);

		JPanel panel_6 = new JPanel();
		panel_2.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 3, 0, 0));

		// resets the JTextAreas 'paid' and 'change'.
		JButton btnDelete = new JButton("DEL");
		btnDelete.setForeground(Color.WHITE);
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					paid.setText("0.0");
					change.setText("0.0");
				} catch (Exception e1) {
					System.err.println("Resetting JTextAreas 'paid' and 'change' failed ");
					e1.printStackTrace();
				}
			}
		});
		btnDelete.setBackground(Color.RED);
		panel_6.add(btnDelete);

		JButton button_5 = new JButton("0");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAmount("0");
			}
		});
		panel_6.add(button_5);

		JButton button_15 = new JButton(",");
		panel_6.add(button_15);

		JPanel panel_10 = new JPanel();
		panel_2.add(panel_10);
		panel_10.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel BottomPanel = new JPanel();
		paymentGUIPanel.add(BottomPanel);
		BottomPanel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_11 = new JPanel();
		BottomPanel.add(panel_11);
		panel_11.setLayout(new GridLayout(1, 0, 0, 0));

		JPanel panel_12 = new JPanel();
		BottomPanel.add(panel_12);
		panel_12.setLayout(new GridLayout(0, 4, 0, 0));

		JButton btnBack = new JButton("Back");
		panel_12.add(btnBack);
		btnBack.setBackground(Color.YELLOW);

		Component glue = Box.createGlue();
		panel_12.add(glue);

		Component glue_1 = Box.createGlue();
		panel_12.add(glue_1);

		JButton btnEndSale = new JButton("End Sale");
		panel_12.add(btnEndSale);
		btnEndSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				finalizeSale();
			}
		});
		btnEndSale.setForeground(Color.WHITE);
		btnEndSale.setBackground(Color.GREEN);

		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				back();
			}
		});

		JPanel panel_13 = new JPanel();
		BottomPanel.add(panel_13);
		panel_13.setLayout(new GridLayout(1, 0, 0, 0));
	}

	/**
	 * adds the pressed number to the 'paid' JTextArea.
	 */
	private void enterAmount(String amount)
	{
		try {
			String oldAmount = String.valueOf( (int) Double.parseDouble((paid.getText())));
			String newAmount = oldAmount+amount;
			double newAmountDouble = Double.parseDouble(newAmount);
			paid.setText(String.valueOf(newAmountDouble));
			enterPayment();
		} catch (NumberFormatException e) {
			System.err.println("adding to 'paid' failed");
			e.printStackTrace();
		}
	}

	/**
	 * adds a quickamount to the current 'paid' amount.
	 */
	private void enterQuickAmount(String quickAmount)
	{
		try {
			double oldAmount = Double.parseDouble(paid.getText());
			double newAmount = oldAmount + Double.parseDouble(quickAmount);
			paid.setText(String.valueOf(newAmount));
			enterPayment();
		} catch (NumberFormatException e) {
			System.err.println("adding quickamount failed");
			e.printStackTrace();
		}
	}

	/**
	 * calculates changemoney
	 */
	private void enterPayment()
	{
		try {
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			if (Double.parseDouble(paid.getText())-sale.getTotal() > 0)
			{
				change.setText(String.valueOf( Double.parseDouble(paid.getText())-sale.getTotal()));
			}
		} catch (NumberFormatException e) {
			System.err.println("calculating change failed");
			e.printStackTrace();
		}
	}

	/**
	 * Updates the sales list JTextArea
	 */
	public void updateSalesList()
	{
		try {
			salesList.setText("");
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			ArrayList<PartSale> partSales = sale.getPartsales();
			for(PartSale partSale : partSales)
			{
				salesList.insert((partSale.getAmount() + " x " + partSale.getProduct().getName() + " : " + partSale.getPrice() + " kr. \n"), 0);
			}
			total.setText(String.valueOf(sale.getTotal()));
		} catch (Exception e) {
			System.err.println("update saleslist failed");
			e.printStackTrace();
		}
	}

	/**
	 * Finalizes (completes) the sale and returns the user to his saleGUIPanel.
	 */
	private void finalizeSale()
	{
		try {
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			SaleCtr.getInstance().finalizeSale(sale);
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getPaymentGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getSaleGUIPanels().get(employee.getLogin()).repaint();
			MainFrame.getSaleGUIs().get(employee.getLogin()).updateSalesList();

			total.setText("0.0");
			paid.setText("0.0");
			change.setText("0.0");
			System.out.println("Sale Finalized!");

			Sale newSale = SaleCtr.getInstance().createSale(employee);
			MainFrame.getSaleGUIs().get(employee.getLogin()).setSale(newSale);	
			MainFrame.getSaleGUIs().get(employee.getLogin()).updateSalesList();
			System.out.println("New Sale Started");	

		} catch (Exception e) {
			System.err.println("Oops, something went wrong. Sale is NOT completed!");
			e.printStackTrace();
		}
	}

	/**
	 * returns the user to his saleGUIPanel.
	 */
	private void back()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getPaymentGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getSaleGUIPanels().get(employee.getLogin()).repaint();
			MainFrame.getSaleGUIs().get(employee.getLogin()).updateSalesList();
		} catch (Exception e) {
			System.err.println("Return to saleGUI failed");
			e.printStackTrace();
		}
	}
}
